/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.swt.statisch;

import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;

import de.mnit.basis.daten.struktur.schluessel.S_SchluesselListe;
import de.mnit.basis.daten.struktur.schluessel.SchluesselListe;
import de.mnit.basis.swt.konstant.SWT_SCHRIFT;
import de.mnit.basis.swt.konstant.SWT_SCHRIFTSTIL;

/**
 * @author Michael Nitsche
 * 18.01.2010	Erstellt
 */
public class SchriftManager {

	private static S_SchluesselListe<String,Font> schriften=SchluesselListe.neu();


	public static Font gSchrift(Device device, SWT_SCHRIFT schrift, Integer groesse, SWT_SCHRIFTSTIL... stile) {
		return iSchrift(device, schrift, groesse, stile);
	}

	public static Font gSchrift(Device device, FontData data) {
		return iSchrift(device, data);
	}

	// INTERN

	private static Font iSchrift(Device device, FontData data) {
		String schluessel=iSchluessel(device, data);
		if(schriften.gSchluessel().kennt(schluessel)) {
			Font font=schriften.gib(schluessel);
			if(font.isDisposed()) {
				schriften.minus(schluessel);
				font=iNeu(schluessel, device, data);
			}
			return font;
		}
		else
			return iNeu(schluessel, device, data);
	}

	private static Font iSchrift(Device device, SWT_SCHRIFT schrift, Integer groesse, SWT_SCHRIFTSTIL... stile) {
		String schluessel=iSchluessel(device, schrift, groesse, stile);
		if(schriften.gSchluessel().kennt(schluessel)) {
			Font font=schriften.gib(schluessel);
			if(font.isDisposed()) {
				schriften.minus(schluessel);
				font=iNeu(schluessel, device, schrift, groesse, stile);
			}
			return font;
		}
		else
			return iNeu(schluessel, device, schrift, groesse, stile);
	}

	private static Font iNeu(String schluessel, Device device, SWT_SCHRIFT schrift, Integer groesse, SWT_SCHRIFTSTIL[] stile) {
		int style=0; //==SWT_SCHRIFTSTIL.NORMAL.swt;
		if(stile!=null)
			for(SWT_SCHRIFTSTIL stil : stile)
				style=style | stil.swt;

		if(schrift==null)
			schrift=SWT_SCHRIFT.STANDARD;

		if(groesse==null)
			groesse=12; //Standard in FontData

		FontData data=new FontData(schrift.schrift, groesse, style);
		return iNeu(schluessel, device, data);
	}

	private static Font iNeu(String schluessel, Device device, FontData data) {
		Font font= new Font(device, data);
		schriften.plus(schluessel, font);
		return font;
	}

	private static String iSchluessel(Device d, SWT_SCHRIFT schrift, Integer groesse, SWT_SCHRIFTSTIL[] stile) {
		return iNull(d)+","+iNull(schrift)+","+iNull(groesse)+","+iStile(stile);
	}

	private static String iSchluessel(Device d, FontData data) {
		return iNull(d)+","+iNull(data);
	}

	private static String iNull(Object o) {
		return o==null ? "null" : o.toString();
	}

	private static String iStile(SWT_SCHRIFTSTIL[] stile) {
		if(stile==null)
			return "null";
		String s="";
		for(SWT_SCHRIFTSTIL stil : stile)
			s+=stil.toString();
		return s;
	}

}
